package com.ja.openchat.ui.chat.adapter.viewholder;

import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.JsonObject;
import com.ja.openchat.R;
import com.ja.openchat.ui.chat.ConversationFragment;
import com.ja.openchat.ui.chat.annotation.EnableContextMenu;
import com.ja.openchat.ui.chat.annotation.MessageContentType;
import com.ja.openchat.ui.chat.remittance.OnTransferRefundCallback;
import com.ja.openchat.ui.chat.remittance.TransferMoneyDetailActivity;
import com.lxj.xpopup.util.XPopupUtils;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgResultBlock;
import com.zy.openchat.core.model.message.UiMessage;
import com.zy.openchat.core.model.remittance.RemittanceDetailInfo;
import com.zy.openchat.core.model.remittance.RemittanceInfo;
import com.zy.openchat.core.model.user.UserInfo;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

@MessageContentType(value = {TelegramManager.MessageType.MessageType_Text_New_Tm, TelegramManager.MessageType.MessageType_Text_Got_Tm, TelegramManager.MessageType.MessageType_Text_Refund_Tm})
@EnableContextMenu
public class TransferMoneyContentViewHolder extends NormalMessageContentViewHolder{
    @BindView(R.id.contentTextView)
    TextView contentTextView;
    @BindView(R.id.contentTextView2)
    TextView contentTextView2;
    @BindView(R.id.tranferLayoout)
    View tranferLayoout;
    @BindView(R.id.dm_center_line)
    View dm_center_line;

    public TransferMoneyContentViewHolder(ConversationFragment fragment, RecyclerView.Adapter adapter, View itemView) {
        super(fragment, adapter, itemView);
    }



    @Override
    protected void onBind(UiMessage message) {
        tranferLayoout.getLayoutParams().width = (int)(XPopupUtils.getAppWidth(fragment.getContext())*0.6);
        if(message.message.tmInfo!=null){
            RemittanceInfo remittanceInfo = message.message.tmInfo;
            contentTextView.setText(String.format("¥%.2f",remittanceInfo.amount));
            handleInfo(remittanceInfo);

        }
    }

    private void handleInfo(RemittanceInfo remittanceInfo){
        RemittanceDetailInfo detailInfo = remittanceInfo.detail;
        if(detailInfo==null){
            queryInfo(remittanceInfo,null);
        }

        if(remittanceInfo.payer == UserInfo.getUserInfo().id&&message.message.isOutgoing()){
            if(detailInfo!=null){
                if(detailInfo.status == 0){
                    contentTextView2.setText(R.string.you_initiated_a_transfer);
                }else if(detailInfo.status==1){
                    contentTextView2.setText(R.string.t_received_text);
                }else{
                    contentTextView2.setText(R.string.t_refunded_text);
                }
            }else{
                contentTextView2.setText(R.string.you_initiated_a_transfer);
            }
        }else{
            if(detailInfo!=null){
                if(detailInfo.status == 0){
                    contentTextView2.setText(R.string.t_please_receive_text);
                }else if(detailInfo.status==1){
                    contentTextView2.setText(R.string.t_paid_for_text);
                }else{
                    contentTextView2.setText(R.string.t_refunded_text);
                }
            }else{
                contentTextView2.setText(R.string.t_please_receive_text);
            }
        }
        boolean isActived = detailInfo!=null&&detailInfo.status!=0;
        tranferLayoout.setActivated(isActived);
        dm_center_line.setBackgroundColor(ContextCompat.getColor(fragment.getContext(),isActived?R.color.white:R.color.color_FFC689));
    }

    private void queryInfo(RemittanceInfo remittanceInfo, OnTransferRefundCallback callback){
        if(remittanceInfo==null) return;
        TelegramManager.getTelegramManager().queryTransferMoney(fragment.getContext(), remittanceInfo.remittanceId, new TgResultBlock<RemittanceDetailInfo>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, RemittanceDetailInfo obj) {
                if(obj!=null){
                    obj.remittanceId = remittanceInfo.remittanceId;
                }
                remittanceInfo.detail = obj;
                handleInfo(remittanceInfo);
                if(callback!=null){
                    callback.doRefund();
                }
            }

            @Override
            public void onTimeout(Map<String, Object> request) {
                if(callback!=null){
                    ToastUtils.showShort(fragment.getString(R.string.request_error));
                }
            }
        });
    }


    @OnClick(R.id.tranferLayoout)
    public void onClick(View view) {
        if(message.message.tmInfo!=null) {
            RemittanceInfo remittanceInfo = message.message.tmInfo;
            if(remittanceInfo.detail!=null){
                TransferMoneyDetailActivity.start(fragment.getContext(),remittanceInfo.detail);
            }else{
                queryInfo(remittanceInfo, new OnTransferRefundCallback() {
                    @Override
                    public void doRefund() {
                        TransferMoneyDetailActivity.start(fragment.getContext(),remittanceInfo.detail);
                    }
                });
            }

        }

    }

}
